package com.antfin.exam.bonus.event;

import com.antfin.exam.bonus.dao.BonusScoreDao;
import com.antfin.exam.bonus.model.ReduceScoreEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class ReduceScoreListener {
    @Autowired
    BonusScoreDao bonusScoreDao;

    @EventListener
    public void reduceScoreListener(ReduceScoreEvent evt){
        ReduceScoreEntity entity = evt.getReduceScoreEntity();
        //直接在数据表上加行级锁
        bonusScoreDao.lockBondScore(entity.getBonusScoreId());
        bonusScoreDao.updateRemainScore(entity.getBonusScoreId(),entity.getRemainScore());
        //写扣分日志到数据表中
        bonusScoreDao.insertBonusScoreLog(entity);
    }
}
